﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.UI.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;

using DotNetNuke.Common.Lists;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Profile;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;
using DotNetNuke.Security.Profile;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Skins.Controls;
using DotNetNuke.UI.Utilities;
using DotNetNuke.UI.WebControls;

using Globals = DotNetNuke.Common.Globals;

using DaMo = VLUNewsData.EntityDataModel;
using BuRU = VLUNewsBusiness.VLURolesAndUsers;
using DotNetNuke.Framework;

namespace VLUNewsUI.VLURolesAndUsers
{
    public partial class RoleSendTo : PortalModuleBase
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            jQuery.RequestDnnPluginsRegistration();

            foreach (DataGridColumn column in dgdRoleToRole.Columns)
            {
                if (ReferenceEquals(column.GetType(), typeof(ImageCommandColumn)))
                {
                    //Manage Delete Confirm JS
                    var imageColumn = (ImageCommandColumn)column;
                    if (imageColumn.CommandName == "Delete")
                    {
                        imageColumn.OnClickJS = Localization.GetString("DeleteItem");
                    }
                    //Localize Image Column Text
                    if (!String.IsNullOrEmpty(imageColumn.CommandName))
                    {
                        imageColumn.Text = Localization.GetString(imageColumn.CommandName, LocalResourceFile);
                    }
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// 	[cnurse]	03/01/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void OnLoad(EventArgs e)
        {
            // Call Page_Load.
            base.OnLoad(e);
            // After Page_Load.
            cmdAdd.Click += cmdAdd_Click;
            dgdRoleToRole.ItemDataBound += new DataGridItemEventHandler(dgdRoleToRole_ItemDataBound);
            dgdRoleToRole.ItemCommand += new DataGridCommandEventHandler(dgdRoleToRole_ItemCommand);
            if (Page.IsPostBack == false)
            {
                BindingData();
            }

            // Release Business Object.
            BuRU.RoleToRole.Release();
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void BindingData()
        {
            // Get roles from the database.
            //int iRoleGroupID = BuRU.RoleAndUser.GetNewsRoleGroupID();
            IList roles = BuRU.RoleToRole.Instance().GetsAllRoles(this.PortalId);
            // Bind data to the FromRole control.
            ddlFromRole.DataSource = roles;
            ddlFromRole.DataTextField = "RoleName";
            ddlFromRole.DataValueField = "RoleID";
            ddlFromRole.DataBind();
            // Bind data to the ToRole control.
            ddlToRole.DataSource = roles;
            ddlToRole.DataTextField = "RoleName";
            ddlToRole.DataValueField = "RoleID";
            ddlToRole.DataBind();

            dgdRoleToRole.DataSource = BuRU.RoleToRole.Instance().GetsAll(this.PortalId);
            Localization.LocalizeDataGrid(ref dgdRoleToRole, LocalResourceFile);
            dgdRoleToRole.DataBind();
        }
       
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// cmdUpdate_Click runs when the Update Button is clicked
        /// </summary>
        /// <history>
        /// 	[cnurse]	03/01/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void cmdAdd_Click(Object sender, EventArgs e)
        {
            int iFromRoleID = int.Parse(ddlFromRole.SelectedValue);
            int iToRoleID = int.Parse(ddlToRole.SelectedValue);
            // Add data.
            int result = BuRU.RoleToRole.Instance().Save(this.PortalId, iFromRoleID, iToRoleID, this.UserId);
            if (result == 1) // Success.
            {
                // Rebinding data to the datagrid.
                dgdRoleToRole.DataSource = BuRU.RoleToRole.Instance().GetsAll(this.PortalId);
                dgdRoleToRole.DataBind();
            }
        }

        void dgdRoleToRole_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            var item = e.Item;
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.SelectedItem)
            {
                var imgColumnControl = item.Controls[0].Controls[0];
                if (imgColumnControl is ImageButton)
                {
                    var delImage = (ImageButton)imgColumnControl;
                    delImage.Visible = true;
                }
                string confirmString = Localization.GetString("ConfirmDeleteMessage", LocalResourceFile);
                ClientAPI.AddButtonConfirm((WebControl)imgColumnControl, confirmString);
            }
        }

        void dgdRoleToRole_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                    grdUsers_DeleteCommand(source, e);
                    break;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// grdUsers_DeleteCommand runs when the icon in the delete column is clicked
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// 	[cnurse]	01/05/2007	Intial documentation
        /// </history>
        /// -----------------------------------------------------------------------------
        private void grdUsers_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            try
            {
                int iRoleToRoleID = Int32.Parse(e.CommandArgument.ToString());
                // Delete Data.
                int result = BuRU.RoleToRole.Instance().Delete(iRoleToRoleID);
                if (result == -1)
                {
                    DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("RoleToRoleError", LocalResourceFile), ModuleMessage.ModuleMessageType.RedError);
                }
                else
                {
                    // Rebinding data to the datagrid.
                    dgdRoleToRole.DataSource = BuRU.RoleToRole.Instance().GetsAll(this.PortalId);
                    dgdRoleToRole.DataBind();
                    // Show message.
                    DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Localization.GetString("RoleToRoleDeleted", LocalResourceFile), ModuleMessage.ModuleMessageType.GreenSuccess);
                }
            }
            catch (Exception exc)
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

    }
}